﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using System.Data;

namespace Runda.CMS.Portal.Finiance
{
    public partial class SaleorCheckOffs : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlMonth.SelectedValue = System.DateTime.Now.Month.ToString();
                ddlYear.SelectedValue = System.DateTime.Now.Year.ToString();
                BindCustomerList(1);
            }
        }
        private void BindCustomerList(int pageindex)
        {
            //List<SqlDbParameter> parms = new List<SqlDbParameter>();

            string Whereclause = "";
            Whereclause += "and IsSaleor = 1 and userid in(select salesman from Cms_Ctrt_Customer) ";
            if (txtContractCodeBasic.Text.Trim() != "")
            {
                Whereclause = " and UserName like '%" + txtContractCodeBasic.Text.Trim() + "%'";
            }
            int RecordCount = 0;
            List<SysUserEntity> list = new SysUserBll().GetAll<SysUserEntity>(Whereclause, "username asc", AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterUsers.DataSource = list;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void RepeaterUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindCustomerList(1);
            }
        }
        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

            BindCustomerList(AspNetPager1.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindCustomerList(1);
        }

        protected void RepeaterUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lblFine = e.Item.FindControl("lblFine") as Label;
            Label lblReward = e.Item.FindControl("lblReward") as Label;
            Label lblPerformanceMonth = e.Item.FindControl("lblPerformanceMonth") as Label;
            Label lblCheckOffsMonth = e.Item.FindControl("lblCheckOffsMonth") as Label;
            Label lblPerformanceYear = e.Item.FindControl("lblPerformanceYear") as Label;
            Label lblCheckOffsYear = e.Item.FindControl("lblCheckOffsYear") as Label;
            Label lblUserID = e.Item.FindControl("lblUserID") as Label;

            Label lblPerformanceMonthTotal = e.Item.FindControl("lblPerformanceMonthTotal") as Label;
            Label lblCheckOffsMonthTotal = e.Item.FindControl("lblCheckOffsMonthTotal") as Label;
            Label lblPerformanceYearTotal = e.Item.FindControl("lblPerformanceYearTotal") as Label;
            Label lblCheckOffsYearTotal = e.Item.FindControl("lblCheckOffsYearTotal") as Label;
            Label lblBalanceMonthTotal = e.Item.FindControl("lblBalanceMonthTotal") as Label;
            Label lblBalanceYearTotal = e.Item.FindControl("lblBalanceYearTotal") as Label;
            Label lblFineMonthTotal = e.Item.FindControl("lblFineMonthTotal") as Label;
            Label lblRewardMonthTotal = e.Item.FindControl("lblRewardMonthTotal") as Label;
            

            Label lblMonthBalance = e.Item.FindControl("lblMonthBalance") as Label;
            Label lblYearBalance = e.Item.FindControl("lblYearBalance") as Label;

            decimal fine=0;
            decimal reward = 0;
            decimal performanceMonth = 0;
            decimal performanceYear = 0;
            decimal checkOffsMonth = 0;
            decimal checkOffsYear = 0;

            decimal FineeMonthTotal = 0;
            decimal RewardMonthTotal = 0;
            decimal performanceMonthTotal = 0;
            decimal performanceYearTotal = 0;
            decimal checkOffsMonthTotal = 0;
            decimal checkOffsYearTotal = 0;

            if (lblCheckOffsMonth != null && lblUserID != null) {
                List<ArmUserPerformanceEntity> PerformanceMonths = new ArmUserPerformanceBll().GetAll<ArmUserPerformanceEntity>(" and Month=" + ddlMonth.SelectedValue.ToString() + " and  Year = " +ddlYear.SelectedValue.ToString() + " and UserID = '" + lblUserID.Text + "'");
                foreach (ArmUserPerformanceEntity entity in PerformanceMonths) {
                    performanceMonth = performanceMonth + entity.Fine + entity.Reward;
                    fine += entity.Fine;
                    reward += entity.Reward;
                }         
                lblPerformanceMonth.Text = performanceMonth.ToString();
                lblFine.Text = fine.ToString();
                lblReward.Text = reward.ToString();

                List<ArmUserPerformanceEntity> PerformanceYears = new ArmUserPerformanceBll().GetAll<ArmUserPerformanceEntity>(" and ((Month<=" + ddlMonth.SelectedValue.ToString() + " and  Year = " + ddlYear.SelectedValue.ToString() + ") or Year<" + ddlYear.SelectedValue.ToString() + ") and UserID = '" + lblUserID.Text + "'");
                foreach (ArmUserPerformanceEntity entity in PerformanceYears)
                {
                    performanceYear = performanceYear + entity.Fine + entity.Reward;
                }
                lblPerformanceYear.Text = performanceYear.ToString();

                List<ArmCheckOffsForMonthEntity> CheckOffsMonths = new ArmCheckOffsForMonthBll().GetAll<ArmCheckOffsForMonthEntity>(" and Month=" + ddlMonth.SelectedValue.ToString() + " and  Year = " + ddlYear.SelectedValue.ToString() + " and UserID = '" + lblUserID.Text + "'");
                foreach (ArmCheckOffsForMonthEntity entity in CheckOffsMonths)
                {
                    checkOffsMonth = checkOffsMonth + entity.RealizeFunds;
                }
                lblCheckOffsMonth.Text = checkOffsMonth.ToString();

                List<ArmCheckOffsForMonthEntity> CheckOffsYears = new ArmCheckOffsForMonthBll().GetAll<ArmCheckOffsForMonthEntity>(" and ((Month<=" + ddlMonth.SelectedValue.ToString() + " and  Year = " + ddlYear.SelectedValue.ToString() + ") or Year<" + ddlYear.SelectedValue.ToString() + ") and UserID = '" + lblUserID.Text + "'");
                foreach (ArmCheckOffsForMonthEntity entity in CheckOffsYears)
                {
                    checkOffsYear = checkOffsYear + entity.RealizeFunds;
                }
                lblCheckOffsYear.Text = checkOffsYear.ToString();
                lblMonthBalance.Text = (performanceMonth - checkOffsMonth).ToString();
                lblYearBalance.Text = (performanceYear - checkOffsYear).ToString();
                
            }
            if (lblPerformanceMonthTotal != null)
            {
                List<ArmUserPerformanceEntity> PerformanceMonthsTotal = new ArmUserPerformanceBll().GetAll<ArmUserPerformanceEntity>(" and Month=" + ddlMonth.SelectedValue.ToString() + " and  Year = " + ddlYear.SelectedValue.ToString());
                foreach (ArmUserPerformanceEntity entity in PerformanceMonthsTotal)
                {
                    performanceMonthTotal = performanceMonthTotal + entity.Fine + entity.Reward;
                    FineeMonthTotal += entity.Fine;
                    RewardMonthTotal += entity.Reward;
                }
                lblPerformanceMonthTotal.Text = performanceMonthTotal.ToString();
                lblFineMonthTotal.Text = FineeMonthTotal.ToString();
                lblRewardMonthTotal.Text = RewardMonthTotal.ToString();

                List<ArmUserPerformanceEntity> PerformanceYearsTotal = new ArmUserPerformanceBll().GetAll<ArmUserPerformanceEntity>(" and ((Month<=" + ddlMonth.SelectedValue.ToString() + " and  Year = " + ddlYear.SelectedValue.ToString() + ") or Year<" + ddlYear.SelectedValue.ToString() + ")");
                foreach (ArmUserPerformanceEntity entity in PerformanceYearsTotal)
                {
                    performanceYearTotal = performanceYearTotal + entity.Fine + entity.Reward;
                }
                lblPerformanceYearTotal.Text = performanceYearTotal.ToString();

                List<ArmCheckOffsForMonthEntity> CheckOffsMonthsTotal = new ArmCheckOffsForMonthBll().GetAll<ArmCheckOffsForMonthEntity>(" and Month=" + ddlMonth.SelectedValue.ToString() + " and  Year = " + ddlYear.SelectedValue.ToString());
                foreach (ArmCheckOffsForMonthEntity entity in CheckOffsMonthsTotal)
                {
                    checkOffsMonthTotal = checkOffsMonthTotal + entity.RealizeFunds;
                }
                lblCheckOffsMonthTotal.Text = checkOffsMonthTotal.ToString();

                List<ArmCheckOffsForMonthEntity> CheckOffsYearsTotal = new ArmCheckOffsForMonthBll().GetAll<ArmCheckOffsForMonthEntity>(" and ((Month<=" + ddlMonth.SelectedValue.ToString() + " and  Year = " + ddlYear.SelectedValue.ToString() + ") or Year<" + ddlYear.SelectedValue.ToString() + ")");
                foreach (ArmCheckOffsForMonthEntity entity in CheckOffsYearsTotal)
                {
                    checkOffsYearTotal = checkOffsYearTotal + entity.RealizeFunds;
                }
                lblCheckOffsYearTotal.Text = checkOffsYearTotal.ToString();

                lblBalanceMonthTotal.Text = (performanceMonthTotal - checkOffsMonthTotal).ToString();
                lblBalanceYearTotal.Text = (performanceYearTotal - checkOffsYearTotal).ToString();
            }
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string Whereclause = "";

            if (txtContractCodeBasic.Text.Trim() != "")
            {
                Whereclause = " and UserName like '%" + txtContractCodeBasic.Text.Trim() + "%'";
            }

            BillBll bll = new BillBll();

            DataTable dt = bll.ExportUserCheckOff(Whereclause,Convert.ToInt16(ddlYear.SelectedValue),Convert.ToInt16(ddlMonth.SelectedValue));

            Export.Export2Excel(dt, "销售员奖惩核销情况一览表", Page.Response);

        }

        private void calculateSummary() {
        
          
        }
    }
}
